﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;

namespace Dynamo
{
    public partial class DynamoViewer : DNNXHR.XHRPortalModule, DotNetNuke.Entities.Modules.IActionable
    {
        protected override void OnInit(EventArgs e)
        {
            engine = new DNNJintEngine(Panel1, this.Page, this);
            engine.Debug = (System.Web.HttpContext.Current.IsDebuggingEnabled || this.UserInfo.IsSuperUser);

            if (this.UserInfo.IsSuperUser)
            {
                Page.ClientScript.RegisterClientScriptInclude("DynamoEditor", ResolveClientUrl("~/DesktopModules/Dynamo/js/DynamoEditor.js"));
                Page.ClientScript.RegisterClientScriptInclude("EditArea", ResolveClientUrl("~/DesktopModules/Dynamo/edit_area/edit_area_full.js"));

                EditorPanel.Visible = true;
            }

            base.ClientMethods.Add("GetScript", new XHRCall(GetScript));
            base.ClientMethods.Add("ExecuteScript", new XHRCall(ExecuteScript));
            base.ClientMethods.Add("SaveScript", new XHRCall(SaveScript));
            base.ClientMethods.Add("ClientID", new XHRCall(SaveScript));
            base.OnInit(e);
        }

        private string GetScript(string Params)
        {
            System.Collections.Hashtable settings = controller.GetTabModuleSettings(base.TabModuleId);
            string script = (string)settings["Script"];
            if (script == null || script.Trim() == "") script = "@{return \"Hello World\";}@";
            return script;
        }
        private string SaveScript(string Params)
        {
            controller.UpdateTabModuleSetting(base.TabModuleId, "Script", HttpUtility.UrlDecode(Params));
            return bool.TrueString;
        }
        private string ExecuteScript(string Params)
        {
            engine = new DNNJintEngine(this.Panel1, this.Page, this);
            engine.Debug = true;
            //engine.JintEngine.Step += new EventHandler<Jint.Debugger.DebugInformation>(JintEngine_Step);
            //engine.JintEngine.Break += new EventHandler<Jint.Debugger.DebugInformation>(JintEngine_Break);
            string script = HttpUtility.UrlDecode(Params);
            return engine.Run(script);
        }
        DotNetNuke.Entities.Modules.ModuleController controller = new DotNetNuke.Entities.Modules.ModuleController();

        DNNJintEngine engine;
        protected void Page_Load(object sender, EventArgs e)
        {
            UpdatePanel2.Visible = Dynamo.AdminPermissions.ScriptingEnabled;
            DisabledPanel.Visible = !Dynamo.AdminPermissions.ScriptingEnabled;
            EditScriptHyperLink.Visible = false;

            if (Dynamo.AdminPermissions.ScriptingEnabled)
            {
                if (base.UserInfo.IsSuperUser)
                {
                    EditScriptHyperLink.Visible = true;
                    EditScriptHyperLink.NavigateUrl = EditUrl("Edit");
                }

                System.Collections.Hashtable settings = controller.GetTabModuleSettings(base.TabModuleId);
                string script = (string)settings["Script"];
                if (script == null || script.Trim() == "") script = "@{return \"Hello World\";}@";

                string result = engine.Run(script);
                if (result != null) this.Panel1.Controls.Add(new LiteralControl(result));
            }
        }

        public DotNetNuke.Entities.Modules.Actions.ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection actions = new ModuleActionCollection();
                actions.Add(GetNextActionID(), "Edit Script", "Edit", "", "", EditUrl("Edit"), false, SecurityAccessLevel.Edit, true, false);
                actions.Add(GetNextActionID(), "Portal Script Settings", "Settings", "", "", EditUrl("Settings"), false, SecurityAccessLevel.Host, true, false);
                return actions;
            }
        }

    }
}